﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FunnyFunction
{
    public class SaoLei
    {
        internal int[,] Arr { get; private set; }
        internal int w { get; private set; }
        internal int h { get; private set; }

        public SaoLei(int x, int y)
        {
            var lei = (x * y) / 5;

            Arr = new int[x, y];
            w = x;
            h = y;
            Random rand = new Random();
            for (int i = 0; i < lei; i++)
            {
                var x1 = rand.Next(x);
                var y1 = rand.Next(y);

                var lCount = GetCount(x1, y1);
                if (lCount > 5)
                {
                    i--;
                    continue;
                }
                Arr[x1, y1] = -1;
            }


            for (int i = 0; i < w; i++)
            {
                for (int j = 0; j < h; j++)
                {
                    if (Arr[i, j] == 0)
                    {
                        Arr[i, j] = GetCount(i, j);
                    }
                }
            }
        }

        private int GetCount(int x1, int y1)
        {
            int sum = 0;
            for (int i = -1; i <= 1; i++)
            {
                for (int j = -1; j <= 1; j++)
                {
                    var xx = x1 + i;
                    var yy = y1 + j;
                    if (xx < 0 || xx >= w || yy < 0 || yy >= h)
                        continue;

                    if (Arr[xx, yy] == -1)
                        sum++;
                }
            }
            return sum;
        }

        public void Print()
        {
            for (int j = 0; j < h; j++)
            {
                for (int i = 0; i < w; i++)
                {
                    var num = Arr[i, j];
                    if (num == -1)
                        Console.Write("♀");
                    else if (num == 0)
                        Console.Write("  ");
                    else
                        Console.Write(GetNum(num));
                }
                Console.WriteLine("");
            }
        }

        private string GetNum(int num)
        {
            switch (num)
            {
                case 1:
                    return "①";
                case 2:
                    return "②";
                case 3:
                    return "③";
                case 4:
                    return "④";
                case 5:
                    return "⑤";
                case 6:
                    return "⑥";
                default:
                    return num.ToString() + " ";
            }
        }
    }

    public class PlaySaoLei
    {
        private int[,] Map;
        private int[,] Open;
        private List<SP> tryList = new List<SP>();
        private int X;
        private int Y;
        public PlaySaoLei(SaoLei sl)
        {
            this.Map = sl.Arr;
            this.X = sl.w;
            this.Y = sl.h;
            Open = new int[X, Y];

            for (int j = 0; j < Y; j++)
            {
                for (int i = 0; i < X; i++)
                {
                    //♥
                    Console.Write("■");
                }
                Console.WriteLine("");
            }

            StartPlay();
        }

        bool Stop = false;
        bool Failed = false;
        private void StartPlay()
        {
            while (!Stop)
            {
                var sp = GetNextPoint();
            }
        }

        private object GetNextPoint()
        {
            if (tryList.Count > 0)
            {
            }

            throw new NotImplementedException();
        }

        private SP RandomPoint()
        {
            List<SP> usList = UnLockList();
            Random rand = new Random();
            return usList[rand.Next(usList.Count)];
        }

        private List<SP> UnLockList()
        {
            throw new NotImplementedException();
        }
    }

    public struct SP
    {
        public int x { get; set; }

        public int y { get; set; }
    }
}
